﻿Imports WiremetModel.Entities.OEM
Imports InputValidation
Imports InputValidation.GeneralRules
Imports InputValidation.GeneralRules.NumericInputRules
Imports System.ComponentModel

Public Class PozycjaZleceniaInputDialog


    Private _unitOfWork As UnitOfWork
    Private _inputValidator As InputValidator



    Private _pozycjaZlecenia As PozycjaZlecenia
    Public Property PozycjaZlecenia() As PozycjaZlecenia
        Get
            Return _pozycjaZlecenia
        End Get
        Set(ByVal value As PozycjaZlecenia)
            _pozycjaZlecenia = value
        End Set
    End Property

    Public Sub New()
        InitializeComponent()
        _unitOfWork = New UnitOfWork()
        _inputValidator = New InputValidator()
        ClearErrorLabels()
        RefreshViewData()
    End Sub

    Public Sub New(ByVal pozycjaZlecenia As PozycjaZlecenia)
        Me.New()

        cmb_Gatunek.SelectedItem = pozycjaZlecenia.Gatunek
        txt_Wymiar.Text = pozycjaZlecenia.Wymiar
        txt_Ilosc.Text = pozycjaZlecenia.Ilosc
        cmb_JednostkaMiary.SelectedItem = pozycjaZlecenia.JednostkaMiary
        txt_TolerancjaDolna.Text = pozycjaZlecenia.TolDolna
        txt_TolerancjaGorna.Text = pozycjaZlecenia.TolGorna
        txt_WytrzymaloscMin.Text = pozycjaZlecenia.WytrzymaloscMin
        txt_WytrzymaloscMax.Text = pozycjaZlecenia.WytrzymaloscMax
        txt_CenaJednostkowa.Text = pozycjaZlecenia.Cena.ToString()
        cmb_Waluta.SelectedItem = pozycjaZlecenia.Waluta
        cmb_Pakowanie.SelectedItem = pozycjaZlecenia.Pakowanie
        cb_CenaPakowania.Checked = pozycjaZlecenia.CenaOpakowania

        'txt_CenaPakowania.Text = pozycjaZlecenia.Pakowanie.Cena
        'cmb_PakowanieWaluta.SelectedItem = 


        cmb_Magazyn.SelectedItem = pozycjaZlecenia.Magazyn
        cb_MaterialPowierzony.Checked = pozycjaZlecenia.PowierzonyMaterial
        cb_Swiadectwo.Checked = pozycjaZlecenia.Swiadectwo
        txt_Uwagi.Text = pozycjaZlecenia.Uwagi

    End Sub



    Private Sub ClearErrorLabels()
        lbl_CenaJednostkowaError.Text = String.Empty
        lbl_GatunekError.Text = String.Empty
        lbl_MagazynError.Text = String.Empty
        lbl_PakowanieError.Text = String.Empty
        lbl_ToleramcjaGornaError.Text = String.Empty
        lbl_TolerancjaDolnaError.Text = String.Empty
        lbl_WydluzenieError.Text = String.Empty
        lbl_WymiarError.Text = String.Empty
        lbl_WytrzymaloscMaxError.Text = String.Empty
        lbl_WytrzymaloscMinError.Text = String.Empty
    End Sub


    Private Sub RefreshViewData()

        Dim normaRepository As New Repository(Of Norma)(_unitOfWork)
        Dim normaList As IEnumerable(Of Norma) = normaRepository.GetAll()
        InitializeInputComboBox(cmb_Norma, normaList)

        Dim gatunekRepository As New Repository(Of Gatunek)(_unitOfWork)
        Dim gatunekList As IEnumerable(Of Gatunek) = gatunekRepository.GetAll()
        InitializeInputComboBox(cmb_Gatunek, gatunekList)

        Dim jednostkaMiaryRepository As New Repository(Of JednostkaMiary)(_unitOfWork)
        Dim jednostkaMiaryList As IEnumerable(Of JednostkaMiary) = jednostkaMiaryRepository.GetAll()
        InitializeInputComboBox(cmb_JednostkaMiary, jednostkaMiaryList)

        Dim walutaRepository As New Repository(Of Waluta)(_unitOfWork)
        Dim walutaList As IEnumerable(Of Waluta) = walutaRepository.GetAll()
        InitializeInputComboBox(cmb_Waluta, walutaList)


        Dim pakowanieRepository As New Repository(Of Pakowanie)(_unitOfWork)
        Dim pakowanieList As IEnumerable(Of Pakowanie) = pakowanieRepository.GetAll()
        InitializeInputComboBox(cmb_Pakowanie, pakowanieList)


        'pakowanie waluta'


        Dim magazynRepository As New Repository(Of Magazyn)(_unitOfWork)
        Dim magazynList As IEnumerable(Of Magazyn) = magazynRepository.GetAll()
        InitializeInputComboBox(cmb_Magazyn, magazynList)

    End Sub


    Private Sub InitializeInputComboBox(ByRef inputComboBox As ComboBox, ByVal data As IEnumerable(Of Object))

        inputComboBox.DataSource = New BindingSource(data, Nothing)
        inputComboBox.AutoCompleteCustomSource.Clear()
        inputComboBox.AutoCompleteCustomSource.AddRange(data.Select(Function(x) x.ToString()).ToArray())
        inputComboBox.SelectedIndex = -1

    End Sub



#Region "Input validation"

    Private Sub cmb_Gatunek_Validating(ByVal sender As Object, ByVal e As CancelEventArgs) Handles cmb_Gatunek.Validating
        Dim notNullableComboBoxValidatingSpecification As New NotNullableComboBoxValidatingSpecification(Of Gatunek)
        _inputValidator.ValidateComboBox(cmb_Gatunek, e, lbl_GatunekError, notNullableComboBoxValidatingSpecification)
    End Sub

    Private Sub txt_Wymiar_Validating(ByVal sender As Object, ByVal e As CancelEventArgs) Handles txt_Wymiar.Validating
        Dim notNullable As New NotNullableTextInputValidatingSpecification()
        Dim numeric As New NumericInputValidatingSpecification()
        Dim numericPositive As New NumericPositiveInputValidatingSpecification()
        _inputValidator.ValidateTextBox(txt_Wymiar, e, lbl_WymiarError, notNullable.And(numeric).And(numericPositive))
    End Sub

    Private Sub txt_Ilosc_Validating(ByVal sender As Object, ByVal e As CancelEventArgs) Handles txt_Ilosc.Validating
        Dim notNullable As New NotNullableTextInputValidatingSpecification()
        Dim numeric As New NumericInputValidatingSpecification()
        Dim numericPositive As New NumericPositiveInputValidatingSpecification()
        _inputValidator.ValidateTextBox(txt_Ilosc, e, lbl_IloscError, notNullable.And(numeric).And(numericPositive))
    End Sub

    Private Sub txt_Wydluzenie_Validating(ByVal sender As Object, ByVal e As CancelEventArgs) Handles txt_Wydluzenie.Validating
        Dim notNullable As New NotNullableTextInputValidatingSpecification()
        Dim numeric As New NumericInputValidatingSpecification()
        Dim numericPositive As New NumericPositiveInputValidatingSpecification()
        _inputValidator.ValidateTextBox(txt_Wydluzenie, e, lbl_WydluzenieError, notNullable.And(numeric).And(numericPositive))
    End Sub

    Private Sub txt_TolerancjaDolna_Validating(ByVal sender As Object, ByVal e As CancelEventArgs) Handles txt_TolerancjaDolna.Validating
        Dim notNullable As New NotNullableTextInputValidatingSpecification()
        Dim numeric As New NumericInputValidatingSpecification()
        _inputValidator.ValidateTextBox(txt_TolerancjaDolna, e, lbl_TolerancjaDolnaError, notNullable.And(numeric))
    End Sub

    Private Sub txt_TolerancjaGorna_Validating(ByVal sender As Object, ByVal e As CancelEventArgs) Handles txt_TolerancjaGorna.Validating
        Dim notNullable As New NotNullableTextInputValidatingSpecification()
        Dim numeric As New NumericInputValidatingSpecification()
        _inputValidator.ValidateTextBox(txt_TolerancjaDolna, e, lbl_TolerancjaDolnaError, notNullable.And(numeric))

        If Not e.Cancel Then
            Dim tolerancjaGorna As New TolerancjaGornaValidatingSpecification(Decimal.Parse(txt_TolerancjaDolna.Text.Trim()))
            _inputValidator.ValidateTextBox(txt_TolerancjaGorna, e, lbl_ToleramcjaGornaError, notNullable.And(numeric).And(tolerancjaGorna))
        End If
    End Sub

    Private Sub txt_WytrzymaloscMin_Validating(ByVal sender As Object, ByVal e As CancelEventArgs) Handles txt_WytrzymaloscMin.Validating
        Dim notNullable As New NotNullableTextInputValidatingSpecification()
        Dim numericInput As New NumericInputValidatingSpecification()
        Dim numericPositive As New NumericPositiveInputValidatingSpecification()
        _inputValidator.ValidateTextBox(txt_WytrzymaloscMin, e, lbl_WytrzymaloscMinError, notNullable.And(numericInput).And(numericPositive))
    End Sub

    Private Sub txt_WytrzymaloscMax_Validating(ByVal sender As Object, ByVal e As CancelEventArgs) Handles txt_WytrzymaloscMax.Validating
        Dim notNullable As New NotNullableTextInputValidatingSpecification()
        Dim numericInput As New NumericInputValidatingSpecification()
        Dim numericPositive As New NumericPositiveInputValidatingSpecification()
        _inputValidator.ValidateTextBox(txt_WytrzymaloscMin, e, lbl_WytrzymaloscMinError, notNullable.And(numericInput).And(numericPositive))

        If Not e.Cancel Then
            Dim wytrzymaloscMaksymalna As New WytrzymaloscMaksymalnaValidatingSpecification(Decimal.Parse(txt_WytrzymaloscMin.Text.Trim()))
            _inputValidator.ValidateTextBox(txt_WytrzymaloscMax, e, lbl_WytrzymaloscMaxError, notNullable.And(numericInput).And(numericPositive).And(wytrzymaloscMaksymalna))
        End If
    End Sub

    Private Sub txt_CenaJednostkowa_Validating(ByVal sender As Object, ByVal e As CancelEventArgs) Handles txt_CenaJednostkowa.Validating
        Dim notNullable As New NotNullableTextInputValidatingSpecification()
        Dim numericInput As New NumericInputValidatingSpecification()
        Dim numericPositive As New NumericPositiveInputValidatingSpecification()
        _inputValidator.ValidateTextBox(txt_WytrzymaloscMin, e, lbl_WytrzymaloscMinError, notNullable.And(numericInput).And(numericPositive))
    End Sub

    Private Sub cmb_Pakowanie_Validating(ByVal sender As Object, ByVal e As CancelEventArgs) Handles cmb_Pakowanie.Validating
        Dim notNullable As New NotNullableComboBoxValidatingSpecification(Of Pakowanie)
        _inputValidator.ValidateComboBox(cmb_Pakowanie, e, lbl_PakowanieError, notNullable)
    End Sub



    Private Sub cmb_Gatuneka_Validated(ByVal sender As Object, ByVal e As EventArgs) Handles cmb_Gatunek.Validated
        lbl_GatunekError.Text = String.Empty
    End Sub

    Private Sub txt_Wymiar_Validated(ByVal sender As Object, ByVal e As EventArgs) Handles txt_Wymiar.Validated
        lbl_WymiarError.Text = String.Empty
    End Sub

    Private Sub txt_Ilosc_Validated(ByVal sender As Object, ByVal e As EventArgs) Handles txt_Ilosc.Validated
        lbl_IloscError.Text = String.Empty
    End Sub

    Private Sub txt_Wydluzenie_Validated(ByVal sender As Object, ByVal e As EventArgs) Handles txt_Wydluzenie.Validated
        lbl_WydluzenieError.Text = String.Empty
    End Sub

    Private Sub txt_TolerancjaDolna_Validated(ByVal sender As Object, ByVal e As EventArgs) Handles txt_TolerancjaDolna.Validated
        lbl_TolerancjaDolnaError.Text = String.Empty
    End Sub

    Private Sub txt_TolerancjaGorna_Validated(ByVal sender As Object, ByVal e As EventArgs) Handles txt_TolerancjaGorna.Validated
        lbl_ToleramcjaGornaError.Text = String.Empty
    End Sub

    Private Sub txt_WytrzymaloscMin_Validated(ByVal sender As Object, ByVal e As EventArgs) Handles txt_WytrzymaloscMin.Validated
        lbl_WytrzymaloscMinError.Text = String.Empty
    End Sub

    Private Sub txt_WytrzymaloscMax_Validated(ByVal sender As Object, ByVal e As EventArgs) Handles txt_WytrzymaloscMax.Validated
        lbl_WytrzymaloscMaxError.Text = String.Empty
    End Sub

    Private Sub txt_CenaJednostkowa_Validated(ByVal sender As Object, ByVal e As EventArgs) Handles txt_CenaJednostkowa.Validated
        lbl_CenaJednostkowaError.Text = String.Empty
    End Sub

    Private Sub cmb_Pakowanie_Validated(ByVal sender As Object, ByVal e As EventArgs) Handles cmb_Pakowanie.Validated
        lbl_PakowanieError.Text = String.Empty
    End Sub


#End Region


    Private Sub btn_Accept_Click(sender As System.Object, e As System.EventArgs) Handles btn_Accept.Click
        Try
            If Not ValidateChildren() Then
                Throw New EntityDataInputException("Podane dane są nieprawidłowe." & vbNewLine &
                                                   "Popraw je i spróbuj ponownie.")
            End If

            Dim confirmationDialogResult As DialogResult = MessageBox.Show("Czy jesteś pewień?", "Uwaga!", MessageBoxButtons.YesNo, MessageBoxIcon.Warning)
            If confirmationDialogResult = Windows.Forms.DialogResult.Yes Then

                With PozycjaZlecenia
                    .Gatunek = CType(cmb_Gatunek.SelectedItem, Gatunek)
                    .Wymiar = txt_Wymiar.Text.Trim()
                    .Ilosc = txt_Ilosc.Text.Trim()
                    .JednostkaMiary = CType(cmb_JednostkaMiary.SelectedItem, JednostkaMiary)
                    .Wydluzenie = txt_Wydluzenie.Text.Trim()
                    .TolDolna = txt_TolerancjaDolna.Text.Trim()
                    .TolGorna = txt_TolerancjaGorna.Text.Trim()
                    .WytrzymaloscMin = txt_WytrzymaloscMin.Text.Trim()
                    .WytrzymaloscMax = txt_WytrzymaloscMax.Text.Trim()
                    .Cena = Double.Parse(txt_CenaJednostkowa.Text.Trim())
                    .Waluta = CType(cmb_Waluta.SelectedItem, Waluta)
                    .Pakowanie = CType(cmb_Pakowanie.SelectedItem, Pakowanie)
                    .CenaOpakowania = cb_CenaPakowania.Checked
                    ' waluta za opakowanie
                    .Magazyn = CType(cmb_Magazyn.SelectedItem, Magazyn)
                    .PowierzonyMaterial = cb_MaterialPowierzony.Checked
                    .Swiadectwo = cb_Swiadectwo.Checked
                    .Uwagi = txt_Uwagi.Text
                End With

                DialogResult = Windows.Forms.DialogResult.OK
            End If
        Catch ex As EntityDataInputException
            MessageBox.Show(ex.Message, "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning)
        Catch ex As Exception
            MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Sub

    Private Sub btn_Cancel_Click(sender As System.Object, e As System.EventArgs) Handles btn_Cancel.Click
        _unitOfWork.Dispose()
        DialogResult = Windows.Forms.DialogResult.Cancel
    End Sub
End Class